package tsb.trinhnx;

import java.util.ArrayList;
import java.util.List;

public class Generation {

	static List<String> list = new ArrayList<String>();
	static int stepCounter = 0;

	public static void main(String[] args) {
		Generation main = new Generation();
		//		main.getString("", 20);
		//		System.out.println("called: " + stepCounter);
		//		for (String string : list) {
		//			System.out.println(string);
		//		}
		long start = System.nanoTime();
		System.out.println("start: " + start);
		main.getString("", 20);
		System.out.println("end: " + (System.nanoTime() - start));
		start = System.nanoTime();
		System.out.println("start: " + start);
		main.generation();
		System.out.println("end: " + (System.nanoTime() - start));
	}

	private void generation() {
		final Integer a[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		// printArray(a);
		boolean isStop = false;
		while (!isStop) {
			int i = a.length - 1;
			while (i >= 0 && a[i] == 1) {
				i--;
			}
			if (i < 0)
				isStop = true;
			else {
				a[i] = 1;
				for (int j = i + 1; j < a.length; j++) {
					a[j] = 0;
				}
				//printArray(a);
			}
		}
	}

	private void getString(String s, int digit) {
		stepCounter++;
		if (digit == 0) {
			list.add(s);
		} else {
			getString(s + "0", digit - 1);
			getString(s + "1", digit - 1);
		}
	}

	private <T> void printArray(T[] data) {
		for (T t : data) {
			System.out.printf("%s", t.toString());
		}
		System.out.println();
	}
}
